App.directive('edMenu', ['$window','$compile',function($window,$compile) {
        return {
            restrict : 'A',
            link : function(scope, element, attrs) {
                var optionName = attrs.edMenu || 'menu';
                var menuOptions = scope[optionName];
                if (!menuOptions) {
                    scope[optionName] = menuOptions = {};
                }
                //每个项点击的事件
                scope[optionName].ms = scope.model;

                var menuUl = document.getElementById(attrs.target+"_ul");
                if(!menuUl && scope.model) {
                    $('body').append(
                        $compile("<ul class='edMenu' id='" + attrs.target + "_ul' style='display: none'><li ng-repeat='m in "+optionName+".ms' ng-click='fn($event,m.name,\""+optionName+"\")'>{{m.name}}</li></ul>")(scope)
                    );
                }

                var menuElement = angular.element(document.getElementById(attrs.target+"_ul"));
                if(!menuElement.get(0)){
                    return;
                }
                function open(event, element) {
                    //先隐藏所有
                    $(".edMenu").hide();

                    var x = event.clientX;
                    var y = event.clientY;
                    var _showLeft = x+15;
                    var _showTop = y+5;

                    //$(menuElement).css('top',_showTop).css('left',_showLeft).css('background', 'rgba(250, 250, 250,0.9)').show();
                    $(menuElement).css('top',_showTop).css('left',_showLeft).show();
                };
                function close(element) {
                    $(menuElement).hide();
                };

                element.bind('contextmenu', function(event) {
                    scope.$apply(function() {
                        event.preventDefault();
                        open(event, menuElement);
                    });
                });

                //窗口绑定点击事件 隐藏右键菜单
                angular.element($window).bind('click', function(event) {
                    scope.$apply(function() {
                        //event.preventDefault();
                        close(menuElement);
                    });
                });
            }
        };
    }]);